package com.xxd.gulimail.product.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.xxd.gulimail.product.entity.BrandEntity;
import com.xxd.gulimail.product.vo.BrandVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xxd.gulimail.product.entity.CategoryBrandRelationEntity;
import com.xxd.gulimail.product.service.CategoryBrandRelationService;
import com.xxd.common.utils.PageUtils;
import com.xxd.common.utils.R;


/**
 * 品牌分类关联
 *
 * @author xxd
 * @email 2132862687@qq.com
 * @date 2022-09-26 10:32:57
 */
@RestController
@RequestMapping("product/categorybrandrelation")
public class CategoryBrandRelationController {
    @Autowired
    private CategoryBrandRelationService categoryBrandRelationService;

/**
 * @Description:获取品牌列表
 * @Author: xuxd
 * @param
 * @Date: 2022/10/17 21:40
 *     product/categorybrandrelation/brands/list?t=1666013944696&catId=225
 */
@GetMapping(value = "/brands/list")
public R brandlist(@RequestParam(value = "catId",required = true) Long catId) {
    List<BrandEntity> entities = categoryBrandRelationService.getBrandsByCatId(catId);

    List<BrandVo> collect = entities.stream().map(obj -> {
        BrandVo brandVo = new BrandVo();
        brandVo.setBrandId(obj.getBrandId());
        brandVo.setBrandName(obj.getName());
        return brandVo;
    }).collect(Collectors.toList());
    return R.ok().put("data", collect);
}
    /**e
     * 列表
     */
    @RequestMapping(value = "/catelog/list",method = RequestMethod.GET)
    public R cateloglist(@RequestParam("brandId") Long brandId) {
        List<CategoryBrandRelationEntity> data = categoryBrandRelationService.list( new QueryWrapper<CategoryBrandRelationEntity>().eq("brand_id",brandId));
        return R.ok().put("data", data);
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = categoryBrandRelationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id) {
        CategoryBrandRelationEntity categoryBrandRelation = categoryBrandRelationService.getById(id);

        return R.ok().put("categoryBrandRelation", categoryBrandRelation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
//        categoryBrandRelationService.save(categoryBrandRelation);
            categoryBrandRelationService.saveDetail(categoryBrandRelation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody CategoryBrandRelationEntity categoryBrandRelation) {
        categoryBrandRelationService.updateById(categoryBrandRelation);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids) {
        categoryBrandRelationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
